﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HMIS.FrameWork.Repository.Nhb;
using HMIS.Infrastructure.Core;
using HMIS.Infrastructure.Core.Repositories;
using HMIS.FrameWork.DomainBase;

namespace HMIS.Infrastructure.Core.Repositories.Impl
{
	public class DictionaryRepositoryImpl : RepositoryNhbImpl<Dictionary>, IDictionaryRepository
	{
		//现在我具备分页，排序，多条件查询，多表关联查询，基本全乎了：）
		public IList<Dictionary> GetPlistByCategoryId(string id, int start, int limit, string sort, string dir, List<DataFilter> filters, out long total)
		{
			sort = "d." + sort;
			var strFilter = base.GetHqlstrByExtFilter(filters, "d");//通过刚才基类的方法把filters转换成HQL字符串

			var query = Session.CreateQuery(@"select d from Dictionary as d left join fetch d.Category where d.Category.Id=:Id"//改了下这里，搞成多表查询
							  + (string.IsNullOrEmpty(strFilter) ? string.Empty : " and " + strFilter)//这里把组合查询字符串加进去
							  + " order by " + sort + " " + dir)
							  .SetString("Id", id)
							  .SetFirstResult(start)
							  .SetMaxResults(limit);

			total = Session.CreateQuery(@"select count(*) from Dictionary as d where d.Category.Id=:Id"
				+ (string.IsNullOrEmpty(strFilter) ? string.Empty : " and " + strFilter))//别忘记这里也要加哟
				.SetString("Id", id)
				.UniqueResult<long>();

			return query.List<Dictionary>();
		}

		public IList<Dictionary> GetPlistByCategoryCode(string TreeCode, string query, int start, int limit, string sort, string dir, List<DataFilter> filters, out long total)
		{
			sort = string.IsNullOrEmpty(sort) ? " order by d.Index asc" : "order by d." + sort + " " + dir;
			var strFilter = base.GetHqlstrByExtFilter(filters, "d");//通过刚才基类的方法把filters转换成HQL字符串

			var q = Session.CreateQuery(@"select d from Dictionary as d left join fetch d.Category 
													where d.Category.TreeCode=:TreeCode 
													and (lower(d.Name) like lower(:query) 
														or lower(d.Code) like lower(:query)
														or lower(d.InputCode1) like lower(:query) 
														or lower(d.InputCode2) like lower(:query) 
														or lower(d.InputCode3) like lower(:query)) "//助记码
							+ (string.IsNullOrEmpty(strFilter) ? string.Empty : " and " + strFilter)//这里把组合查询字符串加进去
							+ sort)
							.SetString("query", "%" + query + "%")
							.SetString("TreeCode", TreeCode);

			if (limit > 0)
				q.SetFirstResult(start).SetMaxResults(limit);

			total = Session.CreateQuery(@"select count(*) from Dictionary as d 
													where d.Category.TreeCode=:TreeCode 
													and (lower(d.Name) like lower(:query) 
														or lower(d.Code) like lower(:query)
														or lower(d.InputCode1) like lower(:query) 
														or lower(d.InputCode2) like lower(:query) 
														or lower(d.InputCode3) like lower(:query)) "//助记码
							+ (string.IsNullOrEmpty(strFilter) ? string.Empty : " and " + strFilter))//别忘记这里也要加哟
							.SetString("query", "%" + query + "%")
							.SetString("TreeCode", TreeCode)
							.UniqueResult<long>();

			return q.List<Dictionary>();
		}

		/// <summary>
		/// 返回所有字典
		/// </summary>
		/// <returns></returns>
		public IList<Dictionary> GetPlistAll()
		{
			var q = Session.CreateQuery(@"select d from Dictionary as d inner join fetch d.Category order by d.Category, d.Index");
			return q.List<Dictionary>();
		}

		public bool IsFieldExist(string fieldName, string fieldValue, string id)
		{
			return base.IsFieldExist(fieldName, fieldValue, id, null);
		}
	}
}
